<?php

include_once('/application/helpers/LatLng.php');
include_once('/application/helpers/Image.php');
include_once('/application/helpers/Bean.php');
include_once('/application/helpers/read_OMWPTConfigFile.php');
include_once('/OmwptConfig.php');
include_once('/application/helpers/Cropper.php');
include_once('/application/helpers/runSplatPowerCalc.php');

/*
 * Created on Dec 1, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

class Map_Controller extends Website_Controller {
        
	public function index() {
	
	  //Title         
		$this->template->title = 'Map::OMWPT';
		$this->template->bodySettings = 'onload="initialize()" onunload="GUnload()"';
		
		// Head
		$newHead = new View('pages/mapHead');
		// Body
		$this->template->content = new View('pages/mapContent');
		
		if (empty ($_POST['radioArray'])) {
			
			//Load the initial map with default coordinates
      $newHead->centerPos = new LatLng(35.913679,-79.055927);		
      $newHead->image = array();
		
		} else {
			
			$antennae = Map_Controller::arrStr2AntArr($_POST['radioArray']);
			$image = array();
			$lats = 0;
			$lngs = 0;
			
			
			
			for($j = 0; $j < count($antennae); $j+=1){
				
				// Get coordinates
				$latlng = $antennae[$j]->latlng;
				$height = $antennae[$j]->height;
				$erp = $antennae[$j]->erp;
				$freq = $antennae[$j]->freq;
				
				// Send info to Splat
				$output = Map_Controller::calculateImage($j, $latlng, $height, $erp, $freq);
				
				// change the lng sign to use it with google
				$latlng->lng = $latlng->lng * -1;
				
				// Add to calculate center
				$lats += $latlng->lat;
				$lngs += $latlng->lng;
				
				// Crop Image and Set Image info
				$tempImage = Crop($output->image->getImagePath(), $latlng, $output->image->getLower(), $output->image->getUpper());
				
				$image[] = Map_Controller::adjustImgFilename($tempImage);
			
			}
			
				$newHead->centerPos = new LatLng(($lats/count($antennae)),($lngs/count($antennae)));
			
				$newHead->image = $image;
			
			//Get the coords from the form
//			$lat = $_POST['lat'];
//			$lng = $_POST['lng'];
//			$lng = $lng * -1;
			
//			$centerPos = new LatLng($lat,$lng);
			
//			$newHead->centerPos = new LatLng($centerPos->lat, $centerPos->lng);
			
			// throw new Exception($newLower->lng);
			
			//Send the info to splat calculation
//			$newOutput = Map_Controller::calculateImage();
			
			//$newImage = $newOutput->image;
			
//			$newImage = Crop($newOutput->image->getImagePath(), $centerPos, $newOutput->image->getLower(), $newOutput->image->getUpper());
			
			$newHead->imageLocation = "/omwpt/splat/data/image.png"; //$newImage->getImagePath();
			
			//throw new Exception("Lower Bounds: " . $newImage->getLower()->lat . ", " . $newImage->getLower()->lng . " Upper Bound: " . $newImage->getUpper()->lat . ", " . $newImage->getUpper()->lng);
			
//			$newHead->sw = $newImage->getLower(); // $newLower; // new LatLng(($centerPos->lat - 1),($centerPos->lng - 2));
//			$newHead->ne = $newImage->getUpper(); // $newUpper; // new LatLng(($centerPos->lat + 1),($centerPos->lng + 2)); 
			
		}
		
		$this->template->head = $newHead;
		
	}
	
	function adjustImgFilename($image){
		
		$name = $image->getImagePath();
		$name = substr($name,strlen($_SERVER['DOCUMENT_ROOT']));
		$image->setImagePath($name);
		return $image;
	
	}
	
	function arrStr2AntArr($arrStr){
		
		$arr = explode(',',$arrStr);
		$antArr = array();
		
		for ($i = 0; $i <= count($arr) - 6; $i+=6){
			$ant = new Antenna();
			$ant->latlng = new LatLng($arr[$i+1], $arr[$i+2]);
			$ant->height = $arr[$i+3];
			$ant->erp = $arr[$i+4];
			$ant->freq = $arr[$i+5];
			$antArr[] = $ant;
		}
		
		return $antArr;
		
	}

	/**
	 * This function 
	 */
	function calculateImage($antNum, $latlng, $height, $erp, $freq) {
	
		
		$parameters = new SplatPowerCommandParams();
		
		$parameters->workingDirectory = OmwptConfig::workingDirectory;
		$parameters->splatPath = OmwptConfig::splatPath;
		$parameters->transmitterFileName = OmwptConfig::transmitterDataName;
		$parameters->receiverHeight = $height;
		$parameters->range = OmwptConfig::propRange;
		$parameters->imageFileName = OmwptConfig::imagePath . $antNum;
		$parameters->erp = $erp;
		$parameters->alpha = 0.5;
		$parameters->convertPath = OmwptConfig::imageMagickPath;
		$parameters->transLatLng = $latlng;
		$parameters->freq = $freq;
		$parameters->climate = OmwptConfig::climate;
		$parameters->polar = OmwptConfig::polarization;
	
		$output = splatPowerCalculation :: runSplatPowerCalculation($parameters);
		
		return $output;

	}
}
?>
